<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="openAlert">增加停车计费规则</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
        />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" prop="chargeType" />
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editButton(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delelteButton(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="float: right;margin-top: 10px;">
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="params.pageSize"
          @current-change="currentChange"
        /></div>
    </div>
    <!-- 在子组件中加上sync修饰符表示可以允许修改 -->
    <AddAlert ref="addRule" :dialog-visible.sync="dialogVisible" @getRuleList="getRuleList" />
  </div>
</template>

<script>
import { utils, writeFileXLSX } from 'xlsx'
import { getRuleListApi, deleteRuleApi } from '@/api/cardRule'
import AddAlert from '@/views/Car/CarRule/component/AddAlert.vue'
export default {
  name: 'Building',
  components: {
    AddAlert
  },
  data() {
    return {
      // 控制弹框的展开和关闭
      // id: 0,
      list: {},
      dialogVisible: false,
      params: {
        page: 1,
        pageSize: 2
      },
      ruleList: [],
      total: 0,
      excelData: {}
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    // 编辑按钮
    editButton(id) {
      this.dialogVisible = true
      this.$refs.addRule.getParkingRule(id)
      this.$refs.addRule.title = '编辑标题'
    },
    // 删除按钮
    delelteButton(id) {
      // console.log(id)
      this.$confirm('确定删除吗?', '温馨提示').then(async() => {
        // console.log(1)
        await deleteRuleApi(id)
        this.$message.success('删除成功')
        if (this.ruleList.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        this.getRuleList()
      }).catch(() => {})
    },
    // 打开弹窗
    openAlert() {
      this.dialogVisible = true
    },
    // 导出excel
    async exportExcel() {
      const res = await getRuleListApi(this.params)
      // console.log(res)
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表 要求一个对象数组格式
      const worksheet = utils.json_to_sheet(
        res.data.rows
      )
      // const tableHeaderKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      // 表头中文字段value
      const tableHeaderValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [tableHeaderValues], { origin: 'A1' })
      // 导出方法进行导出
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    },
    // 序号问题
    indexMethod(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 获取点击到的页数
    currentChange(page) {
      // console.log(page)
      this.params.page = page
      this.getRuleList()
    },
    // 获取请求渲染页面
    async  getRuleList() {
      const res = await getRuleListApi(this.params)
      // console.log(res)
      this.ruleList = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
</style>
